import { createRouter, createWebHistory } from 'vue-router'
import routerGuard from './routerGuard'

const router = createRouter({
    history: createWebHistory(),
    routes: [
        {
            path: '/',
            redirect: '/home',
        },
        {
            path: '/zheba',
            component: () => import('@/pages/ZheBA/index.vue'),
            meta: {
                title: '浙BA',
                isShow: true,
            },
            children: [
                {
                    path: '',
                    name: 'ZheBA',
                    redirect: '/zheba/amap',
                },
                {
                    path: 'amap',
                    component: () => import('@/pages/ZheBA/zhebaMap.vue'),
                    name: 'ZheBaMap',
                    meta: {
                        title: '浙BA地图',
                        isShow: true,
                    },
                },
            ],
        },
        {
            path: '/home',
            component: () => import('@/pages/Home/index.vue'),
            meta: {
                title: '首页',
                isShow: true,
                requiresAuth: true, // 需要登录
            },
            children: [
                {
                    path: '',
                    name: 'Home',
                    redirect: '/home/mainlyCategory',
                },
                {
                    path: 'mainlyCategory',
                    component: () =>
                        import('@/pages/Home/MainlyCategory/index.vue'),
                    name: 'MainlyCategory',
                },
                {
                    path: 'categoryManage',
                    component: () =>
                        import('@/pages/Home/CategoryManage/index.vue'),
                    name: 'CategoryManage',
                    meta: {
                        title: '分类管理',
                        requiresAuth: true,
                    },
                },
                {
                    path: 'userManage',
                    component: () =>
                        import('@/pages/Home/UserManage/index.vue'),
                    name: 'UserManage',
                    meta: {
                        title: '用户管理',
                        requiresAuth: true,
                        requiresAdmin: true, // 需要管理员权限
                    },
                },
            ],
        },
        {
            path: '/subCategory',
            component: () => import('@/pages/SubCategory/index.vue'),
            props(route) {
                return route.query
            },
            meta: {
                title: '子分类主页',
                isShow: true,
                requiresAuth: true,
            },
            children: [
                {
                    path: '',
                    name: 'SubCategory',
                    redirect: '/subCategory/article',
                },
                {
                    path: 'subCategoryManage',
                    component: () =>
                        import(
                            '@/pages/SubCategory/SubCategoryManage/index.vue'
                        ),
                    name: 'SubCategoryManage',
                    meta: {
                        title: '子分类管理',
                        requiresAuth: true,
                    },
                },
                {
                    path: 'article',
                    component: () =>
                        import('@/pages/SubCategory/Article/index.vue'),
                    name: 'Article',
                    props(route) {
                        return route.query
                    },
                },
            ],
        },
        {
            path: '/login',
            component: () => import('@/pages/Login/index.vue'),
            name: 'Login',
            meta: {
                title: '登录页',
                isShow: false, // 不在导航中显示
                requiresAuth: false,
            },
        },
        {
            path: '/todoTask',
            component: () => import('@/pages/TodoTask/index.vue'),
            meta: {
                title: '任务清单',
                isShow: true,
                requiresAuth: true,
            },
            children: [
                {
                    path: '',
                    name: 'TodoTask',
                    redirect: '/todoTask/todoTaskList',
                },
                {
                    path: 'todoTaskList',
                    component: () => import('@/pages/TodoTask/List/index.vue'),
                    name: 'TodoTaskList',
                    meta: {
                        title: '任务列表',
                        requiresAuth: true,
                    },
                },
                {
                    path: 'todoTaskManage',
                    component: () =>
                        import('@/pages/TodoTask/Manage/index.vue'),
                    name: 'TodoTaskManage',
                    meta: {
                        title: '任务管理',
                        requiresAuth: true,
                    },
                },
            ],
        },
        {
            path: '/404',
            component: () => import('@/pages/404/index.vue'),
            name: 'NotFound',
            meta: {
                title: '页面不存在',
                isShow: false,
                requiresAuth: false,
            },
        },
        {
            path: '/:pathMatch(.*)*',
            redirect: '/404',
            name: 'Any',
            meta: {
                title: '任意页',
                isShow: false,
                requiresAuth: false,
            },
        },
    ],
})

// 路由守卫
router.beforeEach((to, from, next) => routerGuard(to, from, next))

export default router
